Preamble

Needed libraries for this tutorial:

library(ggplot2)
library(geo)
library(maps)
library(mapdata)
knitr::opts_chunk$set(warning = FALSE,
                      message = FALSE)

Getting example data into R

d <- read.csv("http://www.hafro.is/~einarhj/data/minke.csv",
              stringsAsFactors = FALSE)
d2 <- read.csv("http://data.hafro.is/assmt/2015/cod/summary.csv",
               stringsAsFactors = FALSE)

The minke data:

str(d)

The assessment summary data:

Taken from hafro data site

str(d2)

Links

ggplot: Key components

ggplot has three key components the user needs to specify in order to create a plot (there are lots more of components):

  1. data: Has to be a dataframe

  2. A set of aesthetic mappings [aes] between variables in the data and visual properties

  3. At least one layer which describes how to render each observation. Layers are usually created with a geom function.

key component 1: the data

ggplot(data = d) # minke data

key component 2: some minimum aesthetics

What do put on the x axis and what on the y-axis

ggplot(data = d, aes(x = age, y = length)) # minke data

key component 3: the layer

The shorcut for layer is geom_**. Here we just want points:

ggplot(data = d, aes(x = age, y = length)) + geom_point()

Different syntax, equivalent outcome

... all road leads to Rome:

ggplot(d, aes(x = age, y = length)) + geom_point()
ggplot(d, aes(age, length))         + geom_point()
ggplot()                            + geom_point(data = d, aes(age, length))
ggplot(data = d)                    + geom_point(aes(x = age, y = length))
ggplot(d)                           + geom_point(aes(age, length))
d %>% ggplot()                      + geom_point(aes(age, length))

Can be stored as an object for later use:

p <- ggplot(d, aes(age, length)) + geom_point()

The class:

class(p)

The structure (a bit of latin - not run here):

str(p)

Some additional aesthetics (besides x and y)

colour

p <- ggplot(d)
p + geom_point(aes(age, length, colour = sex))
p + geom_point(aes(age, length, colour = area))

Manual control of colours:

p <- ggplot(d)
p + geom_point(aes(age, length, colour = sex)) +
  scale_colour_manual(values = c("orange","brown"))
p + geom_point(aes(age, length, colour = area)) +
  scale_colour_manual(values = c("orange","brown"))

shape

p + geom_point(aes(age, length, shape = sex))
Exercise
Create a code that results in these plots: wzxhzdk:15

size

p + geom_point(aes(age, length, size = stomach.volume))

To reveal overlays:

p + geom_point(aes(age, length, size = stomach.volume), alpha = 0.6)
p + geom_point(aes(age, length, size = stomach.volume), alpha = 0.3, col = "red")

facetting

Display same graph by subsets of the data:

ggplot(d) + 
  geom_point(aes(age, length, colour = sex)) + 
  facet_wrap(~ area)
Exercise
Create a code that results in this plot: wzxhzdk:19

Add layers

add smoother to a point

p <- ggplot(d, aes(age, length))
p + geom_point() + geom_smooth()
p + geom_point() + geom_smooth(span = 0.1)

Specific models:

p + geom_point() + geom_smooth(method = "lm")

layer: points as jitters

Add a little random noise to the data to avoid overplotting:

p <- ggplot(d, aes(sex, length))
p + geom_point()
p + geom_jitter()

layer: summarise via boxplots or violins

p + geom_boxplot()
p + geom_violin()

layer: overlay data and summary layers

p + geom_boxplot() + geom_jitter()
Exercise
Create the following 3 layer plot: wzxhzdk:25

layer: frequency polygons

p <- ggplot(d, aes(length))
p + geom_freqpoly()
p + geom_freqpoly(aes(colour = sex))

layer: histograms

More details distribution than boxplots do, but more space:

p + geom_histogram()
p + geom_histogram(aes(fill = sex))

Better way for histograms:

p + geom_histogram(aes(fill = sex)) + facet_wrap(~ sex, ncol = 1)

Controlling binwidths:

p + geom_histogram(binwidth = 50)

layer: bar charts

Histograms for discrete data:

ggplot(d, aes(maturity)) + geom_bar()
Exercise
Modify the above code to generate the following: wzxhzdk:31

layer: line

ggplot(d2, aes(Year, Rec)) + geom_line()
ggplot(d2, aes(Year, Rec)) + geom_line() + expand_limits(y = 0)
Exercise
Create code to generate the following plots: wzxhzdk:33 The latter plot includes feature that has not been dealt with in these slides.

layer: path

ggplot(d2, aes(SSB, Fbar)) + geom_line()
ggplot(d2, aes(SSB, Fbar)) + geom_path()

Check out difference of:

?geom_line
?geom_path

Of more use:

ggplot(d2, aes(SSB, Fbar)) + geom_path() + geom_point(aes(colour = Year), size = 3)

Some controls

labels

p <- ggplot(d, aes(age, length, colour = sex)) + geom_point()
p
p + labs(x = "Age [year]", y = "Length [cm]", 
         colour = "Sex", title = "My minke plot")

breaks

Controls which values appear as tick marks

p <- ggplot(d, aes(age, length)) + geom_point() + labs(x = NULL, y = NULL)
p
p +
  scale_x_continuous(breaks = c(5, 10, 15, 20, 25, 30, 35, 40, 45))
p +
  scale_x_continuous(breaks = seq(5, 45, by = 5)) +
  scale_y_continuous(breaks = seq(500, 950, by = 50))
Exercise
Create code that mimics the following breaks: wzxhzdk:39

limits

p <- ggplot(d, aes(maturity, length))
p + geom_jitter()
p + geom_jitter() + ylim(600, 800)
p + geom_jitter() + ylim(NA, 800) # setting only one limit

For descrete variables:

p + geom_jitter() + ylim(600,800) + xlim("immature","mature")

warning

But be careful when using with summary statistics, e.g.:

p + geom_boxplot()
p + geom_boxplot() + ylim(600, 800)

Remedy:

p + geom_boxplot() + coord_cartesian(ylim = c(600, 800))

A little gis

str(island)
p <- ggplot(island, aes(lon, lat)) + labs(x = NULL, y = NULL)
p + geom_point()
p + geom_line()
p + geom_path()
p + geom_path() + coord_map()

On maps

m <- map_data("world")
str(m)
ggplot(m) +
  geom_polygon(aes(long, lat, group = group)) +
  coord_quickmap()

The reslution of "word" is not very high as can be seen if only want denmark:

denmark <- map_data("world", region = "denmark")
ggplot(denmark) +
  geom_polygon(aes(long, lat, group = group)) +
  coord_quickmap()

For higher resolution the object "worldHires" is often sufficient:

denmark <- map_data("worldHires", region = "Denmark") # here the capital
ggplot(denmark) +
  geom_polygon(aes(long, lat, group = group)) +
  coord_quickmap()

Remedy (get rid of "colony"):

i <- denmark$long > 0
denmark <- denmark[i,]
ggplot(denmark) +
  geom_polygon(aes(long, lat, group = group)) +
  coord_quickmap()
Exercise
Play with different map areas


martinpastoors/mptools documentation built on May 9, 2024, 9:29 p.m.